package com.xinzhi.service.impl;

import com.xinzhi.pojo.LoanApplication;
import com.xinzhi.pojo.LoanInterest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class LoanApprovalService {

    @Autowired
    private LoanApplicationService loanApplicationService;

    @Autowired
    private LoanInterestService loanInterestService;

    // 审批贷款申请
    public void approveLoanApplication(Integer applicationId, Date approvalDate, Integer approverId) {
        LoanApplication loanApplication = loanApplicationService.getLoanApplicationById(applicationId);
        if (loanApplication != null) {
            // 更新贷款申请的审批日期、审批人和放款标志
            loanApplication.setApprovalDate(approvalDate);
            loanApplication.setApproverId(approverId);
            loanApplication.setLoanFlag(1); // 设置为已放款状态
            loanApplicationService.updateLoanApplication(loanApplication);

            // 创建并保存贷款利息记录
            LoanInterest loanInterest = new LoanInterest();
            loanInterest.setLoanAccount(loanApplication.getLoanAccount());
            loanInterest.setLoanBalance(loanApplication.getLoanAmount());
            loanInterest.setInterestStartDate(approvalDate); // 利息开始日期为审批日期
            loanInterest.setInterestDueDate(approvalDate); // 利息到期日期为审批日期
            loanInterest.setInterestRate(0.05f); // 利率为5%

            BigDecimal interestAmount = loanInterestService.calculateInterest(loanInterest);
            loanInterest.setInterestAmount(interestAmount);

            // 保存贷款利息记录
            loanInterestService.createLoanInterest(loanInterest);
        }
    }
}
